layui.config({
    base: "../js/"
}).use(['flow', 'form', 'layer', 'upload'], function () {
    var flow = layui.flow,
        form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        $ = layui.jquery;

    //流加载图片
    var imgNums = 15;  //单页显示图片数量
    flow.load({
        elem: '#Images', //流加载容器
        done: function (page, next) { //加载下一页
            $.ajax({
                type: "get",
                url: "/tiSlideshow/querySlideshow",
                datatype: "json",
                success: function (res) {
                    //  console.log(res)
                    //模拟插入
                    var imgList = [], data = res.data;
                    var maxPage = imgNums * page < data.length ? imgNums * page : data.length;

                    setTimeout(function () {
                        for (var i = imgNums * (page - 1); i < maxPage; i++) {
                            imgList.push('' +
                                '<li>' +
                                '<img  layer-src="' + data[i].src + '" src="' + data[i].thumb + '" alt="' + data[i].alt + '" >' +
                                '<div class="operate">' +
                                '<div class="check">' +
                                '<input type="checkbox" name="belle" lay-filter="choose" lay-skin="primary" title="' + data[i].alt + '">' +
                                '</div>' +
                                '<i class="layui-icon img_del" data-imageId=' + data[i].pid + '>&#xe640;</i>' +
                                '</div>' +
                                '</li>');
                        }
                        next(imgList.join(''), page < (data.length / imgNums));
                        form.render();
                    }, 500);
                }, complete: function (XHR, TS) {
                }
            });
        }
    });

    /**
     * 相册 弹出层
     */
    $("body").on("click", "#Images img", function () {
        var index = $(this).parent("li").index();
        parent.showImg(index);
    })

    /**设置图片的高度
     $(window).resize(function(){
        $("#Images li img").height($("#Images li img").width());
    })
     */
    //多图片上传
    upload.render({
        elem: '.uploadNewImg',
        url: '/tiSlideshow/uploadNewImg',
        multiple: true
        , size: 1024 * 10,
        before: function (obj) {
           // console.log(obj)
            //预读本地文件示例，不支持ie8

        },
        done: function (res) {

            //  console.log(res)
            if (res.code == 200) {
                $('#Images').prepend('<li>' +
                    '<img layer-src="' + res.data.slideshowImg + '" src="' + res.data.slideshowThumb + '" alt="' + res.data.slideshowName + '" class="layui-upload-img">' +
                    '<div class="operate"><div class="check">' +
                    '<input type="checkbox" name="belle" lay-filter="choose" lay-skin="primary" title="' + res.data.slideshowName + '">' +
                    '</div><i class="layui-icon img_del">&#xe640;</i></div></li>')
                //设置图片的高度
                //  $("#Images li img").height($("#Images li img").width());
                form.render("checkbox");
            }
        }
    });


    //全选
    form.on('checkbox(selectAll)', function (data) {
        var child = $("#Images li input[type='checkbox']");
        child.each(function (index, item) {
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });

    //通过判断是否全部选中来确定全选按钮是否选中
    form.on("checkbox(choose)", function (data) {
        var child = $(data.elem).parents('#Images').find('li input[type="checkbox"]');
        var childChecked = $(data.elem).parents('#Images').find('li input[type="checkbox"]:checked');
        if (childChecked.length == child.length) {
            $(data.elem).parents('#Images').siblings("blockquote").find('input#selectAll').get(0).checked = true;
        } else {
            $(data.elem).parents('#Images').siblings("blockquote").find('input#selectAll').get(0).checked = false;
        }
        form.render('checkbox');
    })

    /**
     * 批量删除
     */
    $(".batchDel").click(function () {
        var $checkbox = $('#Images li input[type="checkbox"]');
        var $checked = $('#Images li input[type="checkbox"]:checked');
        if ($checkbox.is(":checked")) {
            layer.confirm('确定删除选中的图片？', {title: '提示信息', shade: 0.01}, function (index) {
                $checked.each(function () {
                    $(this).parents("li").hide(1000);
                    setTimeout(function () {
                        $(this).parents("li").remove();
                    }, 950);
                    var layerIndex = layer.load(2, {time: 5 * 1000});
                    $.ajax({
                        type: "PUT",
                        url: "/tiSlideshow/delSlideshow",
                        datatype: "json",
                        data: {
                            slideshowId: $(this).parents("li").find("i.img_del").data("imageid")
                        },
                        success: function (res) {
                            if (res.code == 200) {
                                $.message({
                                    message: res.msg,
                                    type: 'success',
                                    showClose: true
                                });

                            } else {
                                $.message({
                                    message: res.msg,
                                    type: 'warning',
                                    showClose: true
                                });
                            }
                        }, complete: function (XHR, TS) {
                            layer.close(layerIndex);
                            layer.close(index);
                        }
                    });
                })
                $('#Images li input[type="checkbox"],#selectAll').prop("checked", false);
                form.render();
            })
        } else {
            $.message({
                message: '请选择需要删除的图片!',
                type: 'warning',
                showClose: true
            });
        }
    })


    /**
     * 删除单张图片
     */
    $("body").on("click", ".img_del", function () {
        var slideshowId = $(this).data('imageid');
        var _this = $(this);
        layer.confirm('确定删除图片"' + _this.siblings().find("input").attr("title") + '"吗？', {title: '提示信息'}, function (index) {
            //  console.log(slideshowId)
            $.ajax({
                type: "PUT",
                url: "/tiSlideshow/delSlideshow",
                datatype: "json",
                data: {
                    slideshowId: slideshowId
                },
                success: function (res) {
                    if (res.code == 200) {
                        $.message({
                            message: res.msg,
                            type: 'success',
                            showClose: true
                        });
                        _this.parents("li").hide(1000);
                        setTimeout(function () {
                            _this.parents("li").remove();
                        }, 950);
                    } else {
                        $.message({
                            message: res.msg,
                            type: 'warning',
                            showClose: true
                        });
                    }
                }, complete: function (XHR, TS) {
                    layer.close(index);
                }
            });
        });
    })

})